package myself.a002StackAndQueue;

import java.util.Stack;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/22
 * Time 13:40
 * Mail 739153436@qq.com
 */
public class MinStack {

    private Stack<Integer> normal = new Stack<>();
    private Stack<Integer> min = new Stack<>();

    public static void main(String[] args) throws Exception {
        MinStack min = new MinStack();
        min.push(4);
        min.push(3);
        min.push(2);
        min.push(4);
        min.push(1);

        // 1
        System.out.println(min.getMin());

        // 2
        min.pop();
        System.out.println(min.getMin());

        // 3
        min.pop();
        min.pop();
        System.out.println(min.getMin());
    }

    public void push(int ele) {
        if (min.isEmpty()) {
            min.push(ele);
        } else if (ele <= min.peek()) {
            min.push(ele);
        }
        normal.push(ele);
    }

    public int pop() throws Exception {
        if (normal.isEmpty()) {
            throw new Exception();
        }
        if (normal.peek() <= min.peek()) {
            min.pop();
        }
        return normal.pop();
    }

    public int getMin() throws Exception {
        if (min.isEmpty()) {
            throw new Exception();
        }
        return min.peek();
    }

    public boolean isEmpty() {
        return normal.isEmpty();
    }
}
